<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<link rel="stylesheet" href="https://baijunyao.com/statics/prism/prism.min.css" />
<script src="__STATIC__/js/lib/jquery/1.9.1/jquery.js"></script>

<body>

<div>
    <h2>websocketDemo</h2>
    <input id="message">
    <input type="button" id="button" style="margin-left: 10px;" value="发送">
    <input type="button" id="close" style="margin-left: 10px;" value="关闭">
</div>
<div style="width: 100%;text-align: center" id="say">
</div>


</body>
<script>
    //连接localhost:8181lia
    var ws=new WebSocket("ws://192.168.10.82:8989")
    //打开连接
    ws.open=function(e){
        console.log("连接socket")
    };
    //接受服务端发送过来的信息
    ws.onmessage=function(e){
        var d= eval(e.data);
        if(d.type=="login"){
            alert(d.message)
        }
        if(d.type=="say"){
           var str="<label>"+ d.message+"</label></br>";
            $("#say").append(str)
        }
    }
    $(document).ready(function(){
        $("#button").click(function(){
            //发送信息到服务端
            ws.send($("#message").val())
        })
        $("#close").click(function(){
            //关闭socket连接
            ws.close();
        })
    });
</script>

</html>
